package com.xxx.server.service;

import com.xxx.api.GreeterGrpc;
import com.xxx.api.HelloReply;
import com.xxx.api.HelloRequest;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;

/**
 * gRPC server 接口实现类
 */
@GrpcService  // grpc service注解
public class GrpcServerService extends GreeterGrpc.GreeterImplBase {

    @Override
    public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
        String name = req.getName();
        System.out.println("enter method sayHello(). name: " + name);
        HelloReply reply = HelloReply.newBuilder().setMessage("Hello, " + name).build();
        responseObserver.onNext(reply);
        responseObserver.onCompleted();
    }

    @Override
    public void getAge(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
        String name = req.getName();
        System.out.println("enter method getAge(). name: " + name);
        int age;
        if(name.equals("Alice")) {
            age = 18;
        } else if (name.equals("Bob")) {
            age = 20;
        } else {
            age = 0;
        }
        HelloReply reply = HelloReply.newBuilder().setMessage(req.getName() + " age: " + age).build();
        responseObserver.onNext(reply);
        responseObserver.onCompleted();
    }

}
